package com.base.commom;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具类
 *
 * @author chenwei
 * @date 2018年3月28日 上午10:23:15
 */

@SuppressWarnings("all")
public class DateUtils {

	private static final String FORMAT_STR1 = "yyyy-MM-dd";
	private static final String FORMAT_STR2 = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 获取当前日期之前或者之后的日期
	 * 
	 * @author chenwei
	 * @date 2018年3月28日 上午10:23:52
	 * @param beforeOrAfter
	 *            负数代表之前正数代表之后
	 * @return
	 */
	public static String getDateByTopOrDown(int beforeOrAfter) {
		Calendar c = Calendar.getInstance();
		int day = c.get(Calendar.DATE);
		// System.out.println(day);
		c.set(Calendar.DATE, day + beforeOrAfter);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		day = c.get(Calendar.DATE);
		String date = year + "-" + new String().format("%02d", month) + "-" + new String().format("%02d", day);
		return date;
	}

	/**
	 * 获取当前年月
	 * 
	 * @return Date: 2018年1月30日 下午3:27:52
	 * @author Administrator
	 */
	public static String getCurYearAndMonth() {
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM");
		String format = s.format(new Date());
		return format;
	}

	public static String dateToSStr(Date date) {
		if (null == date) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STR1);
		return sdf.format(date);
	}

	public static String dateToLStr(Date date) {
		if (null == date) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STR2);
		return sdf.format(date);
	}
	/**
	 * 根据日期对象和日期格式获取日期字符串
	 * 
	 * @author chenwei
	 * @date 2018年7月14日 上午1:17:15
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToStr(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	public static Date strToSDate(String dateStr) {
		if (null == dateStr || "".equals(dateStr.trim()) || "null".equals(dateStr.trim()))
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STR1);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date strToLDate(String dateStr) {
		if (null == dateStr || "".equals(dateStr.trim()) || "null".equals(dateStr.trim()))
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STR2);
		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date strToDate(String dateStr, String format) throws ParseException {
		try {
			if (null == dateStr || "".equals(dateStr.trim()))
				return null;
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/*
	 * 面前日期
	 */
	public static Date getTime(int days) {
		Calendar today = Calendar.getInstance();
		today.setTimeInMillis(System.currentTimeMillis() - (1000 * 60 * 60 * 24 * days));
		return today.getTime();
	}

	/*
	 * 
	 * 获取当前日期的上一个月的年月 日期
	 */
	public static String getMonthDate() {
		Date month = getMonth(1);
		String format2 = new SimpleDateFormat("yyyy-MM").format(month);
		return format2;
	}

	/**
	 * 根据当前日期减去指定月数返回日期对象
	 * 
	 * @author chenwei
	 * @date 2018年6月2日 上午11:35:22
	 * @param month
	 * @return
	 */
	public static Date getMonth(int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -month);

		return calendar.getTime();
	}

	/**
	 * 当月最后一天
	 * 
	 * @param args
	 * @throws ParseException
	 * @throws Exception
	 */
	public static Date getLastDay() {
		Calendar cal = Calendar.getInstance();

		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		// 当前月的最后一天
		cal.set(Calendar.DATE, 1);
		cal.roll(Calendar.DATE, -1);
		Date endTime = cal.getTime();
		String endTime1 = datef.format(endTime) + " 23:59:59";
		// 当前月的第一天
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTime = cal.getTime();
		String beginTime1 = datef.format(beginTime) + " 00:00:00";

		try {
			return strToLDate(endTime1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 当月第一天
	 * 
	 * @param args
	 * @throws ParseException
	 * @throws Exception
	 */

	public static Date getFirstDay() {
		Calendar cal = Calendar.getInstance();

		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		// 当前月的第一天
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTime = cal.getTime();
		String beginTime1 = datef.format(beginTime) + " 00:00:00";
		try {
			return strToLDate(beginTime1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date date2date(Date date) throws ParseException {
		return strToSDate(dateToSStr(date));
	}
	
	public static String date2StringFormat(SimpleDateFormat campaignSdf, Date date) {
		if (null == date)
			return "";
		return campaignSdf.format(date);
	}
	/**
	 * 根据指定格式格式化日期返回日期字符串
	 * 
	 * @author chenwei
	 * @date 2018年6月2日 上午11:34:15
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static String string2StringFormat(String dateStr, String format) {
		if (null == dateStr || "".equals(dateStr.trim()))
			return null;
		try {
			Date date = strToLDate(dateStr);
			SimpleDateFormat campaignSdf = new SimpleDateFormat(format);
			return date2StringFormat(campaignSdf, date);
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 根据日期字符串,和日期格式,返回日期date对象
	 * 
	 * @author chenwei
	 * @date 2018年6月2日 上午11:42:06
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date string3StringFormat(String dateStr, String format) {
		if (!StringUtils.isNotBlank(dateStr))
			return null;
		try {
			SimpleDateFormat campaignSdf = new SimpleDateFormat(format);
			return campaignSdf.parse(dateStr);
		} catch (Exception e) {
			return null;
		}
	}

	public static String getCurTime() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int date = cal.get(Calendar.DATE);
		int hour = cal.get(Calendar.HOUR);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);
		int milliSecond = cal.get(Calendar.MILLISECOND);
		return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second + ":" + milliSecond;
	}

	public static int getCurYear() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		return cal.get(Calendar.YEAR);
	}

	public static int getCurMonth() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		return cal.get(Calendar.MONTH) + 1;
	}

	public static Date addDate(Date d, long day) {
		long time = d.getTime();
		day = day * 24 * 60 * 60 * 1000;
		time += day;
		return new Date(time);
	}

	public static String addStringDate(String d, int day) {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STR1);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.DATE, day);
		return sdf.format(cal.getTime());
	}

	public static Date addHour(Date d, long hour) throws ParseException {
		long time = d.getTime();
		hour = hour * 60 * 60 * 1000;
		time += hour;
		return new Date(time);
	}

	public static String addDate(String d, int day) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_STR1);
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(d));
		cal.add(Calendar.DATE, day);
		return sdf.format(cal.getTime());
	}
	/**
	 * 日期字符串转日期对象
	 * 
	 * @author chenwei
	 * @date 2018年4月16日 下午3:24:17
	 * @param dateStr 日期字符串
	 * @param pattern 日期格式
	 * @return
	 * @throws ParseException
	 */
	public static Date StringToDate(String dateStr,String pattern) throws ParseException {
		if(!StringUtils.isNotBlank(dateStr)) {
			return null;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		Date parse = dateFormat.parse(dateStr);
		return parse;
	}

	public static int compareTime(Date dt1,Date dt2){
		if (dt1.getTime() > dt2.getTime()) {
			System.out.println("dt1 在dt2前");
			return 1;
		} else if (dt1.getTime() < dt2.getTime()) {
			System.out.println("dt1在dt2后");
			return -1;
		} else {
			return 0;
		}
	}

	/**
	 * date2比date1多的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDays(Date date1,Date date2)
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if(year1 != year2)   //同一年
		{
			int timeDistance = 0 ;
			for(int i = year1 ; i < year2 ; i ++)
			{
				if(i%4==0 && i%100!=0 || i%400==0)    //闰年
				{
					timeDistance += 366;
				}
				else    //不是闰年
				{
					timeDistance += 365;
				}
			}

			return timeDistance + (day2-day1) ;
		}
		else    //不同年
		{
			System.out.println("判断day2 - day1 : " + (day2-day1));
			return day2-day1;
		}
	}
//	public static void main(String[] args) throws Exception {
//		Date string3StringFormat = string3StringFormat("2018-06-02", "yyyy-MM-dd");
//		System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(string3StringFormat));
//	}
}
